package cn.com.generaldata.os.service.indicator_evaluation.incr_analysis;

import org.apache.commons.lang3.Validate;
import org.nutz.dao.sql.Criteria;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.jsme_indicator_system.util.Constants;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 资本贡献率评估指标计算类.
 */
public class LabourContributionCalculateService extends IndicatorEvaluationCalculateService {
	private String alphaIndicatorName = "贡献率alpha";

	private String GOPComparablePriceIndicatorName = "海洋生产总值可比价";

	private String oceanEmployedIndicatorName = ProvinceIndicatorConstant.OCEAN_EMPLOYED_INDICATOR_NAME;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double result = null;
		if (indicator.getSection() == Constants.INDICATOR_LEVEL_PROVINCE) {
			result = calculateProvinceResult(indicator, indicatorAccountVO);
		} else if (indicator.getSection() == Constants.INDICATOR_LEVEL_CITY) {
			// 市指标没确定先保留
		}
		return result;
	}

	private Double calculateProvinceResult(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		Double alpha = getAlpha(indicatorAccountVO);

		Double GOPComparablePrice = getGOPComparablePrice(indicatorAccountVO, indicatorAccountVO.getYear());
		Double lastGOPComparablePrice = getGOPComparablePrice(indicatorAccountVO, indicatorAccountVO.getYear() - 1);

		Double GOPComparablePriceIncr = (GOPComparablePrice - lastGOPComparablePrice) / lastGOPComparablePrice;

		Double oceanEmployedRate = getOceanEmployedRate(indicatorAccountVO);

		return (1 - alpha) * oceanEmployedRate / GOPComparablePriceIncr * 100;
	}

	private Double getAlpha(IndicatorAccountVO indicatorAccountVO) {
		Double alpha = getInputOrAccountingIndicatorValue(indicatorAccountVO, indicatorAccountVO.getYear(),
				alphaIndicatorName);
		return alpha == null ? ProvinceIndicatorConstant.ALPHA : alpha;

	}

	/**
	 * 获得海洋生产总值可比价
	 * 
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double getGOPComparablePrice(IndicatorAccountVO indicatorAccountVO, int year) {
		Integer indicatorId = getIndicatorEvaluationId(GOPComparablePriceIndicatorName,
				indicatorAccountVO.getAreaId());
		Criteria cri = getIndicatorDataCri(indicatorAccountVO, year, indicatorId);
		Double result = getEvaluationIndicatorValue(cri);
		Validate.isTrue(result != null);
		return result;
	}

	/**
	 * 获得涉海就业人员增速
	 * 
	 * @param indicatorAccountVO
	 * @return
	 */
	private Double getOceanEmployedRate(IndicatorAccountVO indicatorAccountVO) {
		Integer indicatorId = getIndicatorInputId(oceanEmployedIndicatorName, null);
		Criteria currentCri = getIndicatorDataCri(indicatorAccountVO, indicatorAccountVO.getYear(), indicatorId);
		Criteria lastCurrentCri = getIndicatorDataCri(indicatorAccountVO, indicatorAccountVO.getYear() - 1, indicatorId);

		Double currentOceanEmployed = getInputIndicatorValue(currentCri);
		Validate.isTrue(currentOceanEmployed != null, "本期涉海就业人数为0，不可以计算");
		Double lastOceanEmployed = getInputIndicatorValue(lastCurrentCri);
		Validate.isTrue(lastOceanEmployed != null, "上期涉海就业人数为0，不可以计算");
		return (currentOceanEmployed - lastOceanEmployed) / lastOceanEmployed;

	}
}
